package org.jeecgframework.web.service.impl;
import java.io.Serializable;
import java.util.Map;
import java.util.UUID;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.service.CbsInstallPackageServiceI;
import org.jeecgframework.web.system.pojo.base.CbsInstallPackageEntity;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("cbsInstallPackageService")
@Transactional
public class CbsInstallPackageServiceImpl extends CommonServiceImpl implements CbsInstallPackageServiceI {

	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((CbsInstallPackageEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((CbsInstallPackageEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((CbsInstallPackageEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(CbsInstallPackageEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(CbsInstallPackageEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(CbsInstallPackageEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,CbsInstallPackageEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{package_type}",String.valueOf(t.getPackageType()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}

	@Override
	public String CalcVersion(String fileType) {
		String maxVersion = "0";
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ");
		sql.append(" 	MAX(version) AS maxVersion ");
		sql.append(" FROM ");
		sql.append(" 	cbs_install_package ");
		sql.append(" WHERE ");
		sql.append(" 	package_type ='").append(fileType).append("' ");
		Map<String, Object> versionMap = super.findOneForJdbc(sql.toString());
		if(null != versionMap && StringUtil.isNotEmpty(versionMap.get("maxVersion"))){
			maxVersion = versionMap.get("maxVersion").toString();
		}
		maxVersion = Integer.valueOf(maxVersion) + 1 + "";
		return maxVersion;
	}

	@Override
	public String getAppFileLogId(String fileType, String version) {
		String fileLogId = "";
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ");
		sql.append(" 	file_log_id AS fileLogId ");
		sql.append(" FROM ");
		sql.append(" 	cbs_install_package cip ");
		sql.append(" WHERE  ");
		sql.append(" 	cip.package_type ='").append(fileType).append("' ");
		if(StringUtil.isNotEmpty(version)){
			sql.append(" AND cip.version ='").append(version).append("' ");
		}else{
			sql.append(" ORDER BY cip.version DESC LIMIT 1 ");
		}
		Map<String, Object> versionMap = super.findOneForJdbc(sql.toString());
		if(null != versionMap && StringUtil.isNotEmpty(versionMap.get("fileLogId").toString())){
			fileLogId = versionMap.get("fileLogId").toString();
		}
		return fileLogId;
	}
}